משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

Similar documents
מבוא לתכנות תוכנית שעור מס. 1 1 דר' דרור טובי, המרכז האוניברסיטאי אריאל בשומרון.

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

תוכנה 1 טיפוסי השפה טיפוסים לא פרימיטיביים הטיפוסים הפרימיטיביים מחרוזות המרה למספרים תרגול 2: טיפוסי שפה, מחרוזות, מערכים ושגיאות

קורס תכנות שיעור שני: שימוש במשתנים,

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

מבוא למדעי המחשב תירגול 2: מבוא למדעי המחשב מ' - תירגול 2

מבוא לתכנות ב- JAVA מעבדה 2

1.1 Your First Program

מבוא למדעי המחשב תירגול 3:

מבוא למדעי המחשב הרצאה 2: ראשונים בשפת C

1.1 Your First Program

1.1 Your First Program

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

1.1 Your First Program

1.1 Your First Program! Naive ideal. Natural language instructions.

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

תוכנה 1 סמסטר א' תשע"א

הנכות 1 םוכיס לוגרת 14 1

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

AP CS Unit 3: Control Structures Notes

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> }

תרגילים ופתרונות בשפת - C הסתעפויות

1.1 Your First Program

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num)

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

1.1 Your First Program

Lecture 2: Intro to Java

A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts,

1.1 Your First Program

מבוא לתכנות ב- JAVA תרגול 7

1.1 Your First Program

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

double float char In a method: final typename variablename = expression ;

Introduction to Computer Science Unit 2. Notes

Important Java terminology

Introduction to Computer Science Unit 2. Notes

DATA TYPES AND EXPRESSIONS

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen

AP Computer Science A. Return values

פתרון מוצע לבחינת מה"ט ב_שפת c מועד אביב תשע"ח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות

חוברת תרגילים לתרגול יסודות התכנות

1.3 Conditionals and Loops

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

תוכנה 1. תרגול 1: סביבת העבודה ומבוא ל- Java

הנכות 1 םוכיס לוגרת 13 1

קורס תכנות רשימה מקושרת דוגמה: חיפוש מעבר על רשימה דוגמא: שחרור רשימה מקושרת דוגמא: הוספת אברים שלא בהתחלה

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

More types, Methods, Conditionals. ARCS Lab.

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1

מבוא למדעי המחשב תרגול 13: עצים בינאריים

Algorithms. Intro2CS week 5

Programming for Engineers in Python

Programming for Engineers in Python

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CS110: PROGRAMMING LANGUAGE I

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

תרגול מספר 3: מערכים

מבוא לתכנות ב- JAVA מעבדה 4

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Variables and data types

קורס תכנות שיעור שישי: מחרוזות, מצביעים

קורס תכנות בשיעור הקודם למדנו על רקורסיה שיעור שישי: מערכים פונקציה רקורסיבית שאלה חישוב נוסחאות רקורסיביות בשפת C

M e t h o d s a n d P a r a m e t e r s

הנכות 1 םוכיס לוגרת 13 1

1.3 Conditionals and Loops

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

תוכנה 1 * לא בהכרח בסדר הזה

Engineering Programming A

קורס תכנות כתובות בזיכרון כתובות בזכרון מצביעים וכתובות מצביעים וכתובות שיעור שביעי: מבנים, הקצאת זיכרון דינאמית האופרטור &

Java Basic Programming Constructs

תוכנה 1 תרגול 2: מערכים ומבני בקרה

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

COMP 110/L Lecture 13. Kyle Dewey

הנכות 1 םוכיס לוגרת 13 1

MODULE 02: BASIC COMPUTATION IN JAVA

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

Calculations, Formatting and Conversions

Lecture 6: While Loops and the Math Class

רזח יליגרתו םי יראני ב ם

C programming. conditional statements

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

CS 61B Data Structures and Programming Methodology. June David Sun

Visual Programming. Lecture 2: More types, Methods, Conditionals

JAVA OPERATORS GENERAL

The Math Class. Using various math class methods. Formatting the values.

H212 Introduction to Software Systems Honors

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

CS 177 Spring 2010 Exam I

Review of Basic Java

Transcription:

משתנים שעור מס. 2 דרור טובי דר' כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

תפקיד המשתנים הצהרה על משתנה השמת ערך במשתנה int a, b, c; a = 1234; b = 99; c = a + b; משתנים מאפשרים לנו לשמור מידע בתוכנית 2

סוגי משתנים הצהרת משתנים סוג משתנה int double boolean char String הצהרת משתנים ערכים מספרים שלמים מספרים ממשיים ערכים בוליאנים תווים סדרת תווים דוגמאות 23 56644 4 2.56 3.1244 112.7, w 2 \n A ABC 23 type variable_name_1, variable_name_2,..., variable_name_n; int a; a= 3; char c; c = 3 ; String s; a= 3; 3

מעקב אחרי ערכי משתנים בתוכנית a b c int a, b; לא מוגדר לא מוגדר a = 1234; לא מוגדר 1234 b = 99; 1234 99 int c = a; 1234 99 1234 a = b; 99 99 1234 b = c; 99 1234 1234 4

int פעולת החיבור עבור Strings ו public class IntAndStrings { public static void main(string[] args) { int a = 1234; int b = 56; System.out.println(a + b); // summation of integers String sa = "1234"; String sb = "56"; System.out.println(sa + sb); // summation of Strings // automatic conversion of int to String String sc = "" + a + b; System.out.println(sc); $ java IntAndStrings 1290 123456 123456 פעולת החיבור מוגדרת הסכום עבור מספרים ושרשור עבור מחרוזות 5

מעבר ממחרוזות למספרים public class Sum { השיטות הפיכת מחרוזת למספר Integet.parseInt() Double.parseDouble() תוכנית המקבלת שני מספרים ומדפיסה את סכומם public static void main(string[] args) { int a = Integer.parseInt( args[0] ); int b = Integer.parseInt( args[1] ); System.out.println("The sum is: " + (a + b) ); $ java Sum 5 4 The sum is: 9 6

פעולות חשבון ב integers הערות בחילוק לא קיימת שארית שארית החילוק שגיאה קדימות ל * קדימות ל / חישוב מבוצע משמאל לימין סגנון כתיבה טוב קדימות ל )( תוצאה 8 2 15 1 2 13 5-4 -4 0 ביטוי 5 + 3 5-3 5 * 3 5 / 3 5 % 3 1 / 0 3 * 5-2 3 + 5 / 2 3 5-2 (3 5) - 4 3 (5 2) טווח המספרים מטיפוס int הוא -2 31 : 2 31-1 7

אופרטור המודולו % במספרים שלמים a % b a ( a / b) * b 17 % 3 = 17 (17/3) * 3 17-5 * 3 8

פעולות חשבון ב double תוצאה 3.171 3.111 3.01e23 1.666666 0.577 Infinity 1.4142 NaN ביטוי 3.141 +.03 3.141 -.03 6.02e23 / 2 5.0 / 3.0 10.0 % 3.141 1.0 / 0.0 Math.sqrt(2.0) Math.sqrt(-1.0) טווח המספרים מסוג double הוא: 4.94065645841246544e-324d to 1.79769313486231570e+308d 9

אופרטור המודולו % במספרים ממשיים a % b a (int)( a/ b) * b 10.0 % 3.141 = 10.0 (int)(10.0/ 3.141) * 3.141 10.0 - (int) 3.184 * 3.141 10.0-3 * 3.141 10

חישוב שורשי הפולינום x 2 +bx+c x 2 +bx+c public class Quadratic { תוכנית לחישוב שורשי הפולינום public static void main(string[] args) { double b = Double.parseDouble( args[0] ); double c = Double.parseDouble( args[1] ); double discriminant = b*b - 4.0*c; double d = Math.sqrt(discriminant); System.out.println( (-b + d) / 2.0); System.out.println( (-b - d) / 2.0); $ java Quadratic -1.0-1.0 1.618033988749895-0.6180339887498949 $ java Quadratic 1.0 1.0 NaN NaN 11

משתנים בוליאניים משתנה יכול לקבל רק אחד משני ערכים:,! פעולות לוגיות &&,, האופרטור && AND האופרטור OR! האופרטור NOT a b a && b a b a b a!a if ( test_grade >= 60 && exercise_grade>=60 ) System.out.println ( You passes the course ); דוגמא 12

אופרטורים להשוואה משמעות y קטן מ x y קטן או שווה ל x y גדול מ x y גדול או שווה x y שווה ל x y שונה מ x אופרטור > >= > >= ==!= דוגמא x < y x <= y x > y x >= y x == y x!= y 13

תוכנית להשוואת מספרים public class IsOrdered { תוכנית הבודקת האם שלושה מספרים מסודרים בסדר עולה public static void main(string[] args) { double a = Double.parseDouble( args[0] ); double b = Double.parseDouble( args[1] ); double c = Double.parseDouble( args[2] ); boolean firsttwo = (a <= b); boolean lasttwo = (b <= c); boolean allthree = firsttwo && lasttwo; System.out.println("Are the numbers ordered? " + allthree); $ java IsOrdered 1 2 2.5 Are the numbers ordered? $ java IsOrdered 1 6 2.5 Are the numbers ordered? 14

שיטות מתמטיות עיקריות double Math.E() Value of e, 2.718282 double Math.PI() Value of pi, 3.14159265 double Math.sin(ar) Returns the sine of ar. double Math.cos(ar) Returns the cosine of ar. double Math.tan(ar) Returns the tangent of ar. מספרים אקראיים קבועים מתמטיים שיטות טריגונומטריות double Math.sqrt(d) Returns the square root of d. t Math.abs(x) Returns absolute value of x with same type as the parameter: int, long, float, or double. t Math.max(x, y) Returns maximum of x and y with same type as the parameter: int, long, float, or double. t Math.min(x, y) Returns minimum of x and y with same type as the parameter: int, long, float, or double. double Math.random() Returns a number x in the range, 0.0 <= x < 1.0. מספרים אקראיים 15

מעבר בין סוגי משתנים double מעבר בטוח אין איבוד מידע int double בטוח לא מעבר יש איבוד מידע int 16

Type conversion - error public class TypeConversion { public static void main(string[] args) { double a = 3.5; int n = a; System.out.println(n); $ javac TypeConversion.java TypeConversion.java:5: possible loss of precision found : double required: int int n = a; ^ 1 error $ 17

Type conversion public class TypeConversion1 { public static void main(string[] args) { double a = 3.5; int n = (int) a; System.out.println(n); $ javac TypeConversion1.java dror@dror-1 ~/java $ java TypeConversion1 3.4 3 18

סיכום מעבר בין משתנים ערך 12345 123 3 4 4 3.3 3.3 0 3 סוג String int int long int double double int int ביטוי 1234 + 56 Integer.parseInt( 123 ); (int) 3.345 Math.Round(3.6758) (int) Math.Round(3.6758) 11 * 0.3 (int) 11 * 0.3 11 * (int) 0.3 (int) (11 * 0.3) 19